﻿/**********************************************************
*
* 模块名称：公用操作基类
* 当前版本：1.0
* 添加人员：楚涛
* 开始时间：2010-10-10
* 版本历史：v1.0
*
***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Database;
using System.Database.Extend;
using LXChuTao.ECG.Demo.Entity;

namespace LXChuTao.ECG.Demo.BLL.SimpleFactory
{
    public class BaseClass
    {
        #region 公共静态属性
        /// <summary>
        /// 当前DbCore实例
        /// </summary>
        protected static DbCore GetDbCore
        {
            get
            {
                return new DbCore(BaseEntity.GetDatabaseType(), BaseEntity.GetConnectionString());
            }
        }
        /// <summary>
        /// 主键唯一ID字符串
        /// </summary>
        public static string GetKeyId
        {
            get { return CommonHelp.GetID25(); }
        }
        #endregion

        #region public virtual GetDataSet/Save/Other 通用方法示例,若被子类覆盖则执行子类方法,未被子类覆盖则执行这里对应的方法
        /// <summary>
        /// 查询示例通用方法
        /// </summary>
        /// <returns>查询数据结果集</returns>
        public virtual DataSet GetDataSet()
        {
            return GetDbCore.Select(T_DEMO.s_C_ID).Add(T_DEMO.s_C_NAME).Add(T_DEMO.s_C_IDCARD)
                    .Add(T_DEMO.s_C_DATE).Add(T_DEMO.s_C_EMAIL).Add(T_DEMO.s_C_STRING).Add(T_DEMO.s_C_INT)
                    .Add(T_DEMO.s_C_FLOAT).Add(T_DEMO.s_C_EIDTDATE)
                    .From(T_DEMO.s_TableName).ExecuteDataSet();
        }
        /// <summary>
        /// 数据库DML操作示例通用方法
        /// </summary>
        /// <returns>DML操作影响的行数</returns>
        public virtual int Save()
        {
            DbCore dbCore = null;
            try
            {
                dbCore = GetDbCore;
                dbCore.Open();
                dbCore.BeginTransaction();
                int count;
                string strId = GetKeyId;
                count = dbCore.InsertInto(T_DEMO.s_TableName).Values(T_DEMO.s_C_ID, strId)
                        .Values(T_DEMO.s_C_NAME, "NameInsert").Values(T_DEMO.s_C_IDCARD, "340221196606066066")
                        .Values(T_DEMO.s_C_EIDTDATE, DateTime.Now)
                        .ExecuteNonQuery();
                count += dbCore.UpdateFrom(T_DEMO.s_TableName).Set(T_DEMO.s_C_EIDTDATE, DateTime.Now)
                        .Set(T_DEMO.s_C_DATE, DateTime.Today).Set(T_DEMO.s_C_NAME, "NameIUpdate")
                        .Where().ConditionAndEqual(T_DEMO.s_C_ID, strId)
                        .ExecuteNonQuery();
                count += dbCore.DeleteFrom(T_DEMO.s_TableName)
                        .Where().ConditionAndGreat(T_DEMO.s_C_EIDTDATE, DateTime.Now)
                        .ExecuteNonQuery();
                dbCore.CommitTransaction();
                dbCore.Close();
                return count;
            }
            catch (Exception ex)
            {
                if (dbCore != null)
                {
                    if (dbCore.IsTransaction) dbCore.RollbackTransaction();
                    dbCore.Close();
                }
                throw ex;
            }
        }
        /// <summary>
        /// 调用存储过程的通用示例
        /// </summary>
        /// <returns>受影响的行数</returns>
        public virtual int Other()
        {
            DbCore dbCore = null;
            try
            {
                dbCore = GetDbCore;
                dbCore.Open();
                dbCore.BeginTransaction();
                int count;
                DBCommandWrapper cmd = dbCore.GetStoredProcCommandWrapper("storedProcedure.NameA");
                //cmd.AddInParameter(
                //cmd.AddOutParameter(
                //cmd.AddParameter(
                count = dbCore.ExecuteNonQuery(cmd);
                cmd = dbCore.GetStoredProcCommandWrapper("storedProcedure.NameB");
                //cmd.AddInParameter(
                count += dbCore.ExecuteNonQuery(cmd);
                //count += dbCore.ExecuteStoredProcedure("storedProcedure.NameC");
                dbCore.CommitTransaction();
                dbCore.Close();
                return count;
            }
            catch (Exception ex)
            {
                if (dbCore != null)
                {
                    if (dbCore.IsTransaction) dbCore.RollbackTransaction();
                    dbCore.Close();
                }
                throw ex;
            }
        }
        #endregion

        #region public virtual string Method1/Method2/Method3 其它方法
        public virtual string Method1()
        {
            return "BaseClass.Method1";
        }
        public virtual string Method2()
        {
            return "BaseClass.Method2";
        }
        public virtual string Method3()
        {
            return "BaseClass.Method3";
        }
        #endregion
    }
}
